<?php
/**
 * 管理后台首页文件
 * 这个页面是博客系统的管理后台主页面，显示网站统计数据和快速操作入口
 * 只有登录用户才能访问这个页面
 */

// 引入系统初始化文件，这个文件包含了博客系统的所有核心功能
require_once '../includes/init.php';

// 检查用户是否已经登录，如果没有登录就跳转到登录页面
// $blog是在init.php中创建的全局BlogSystem实例
if (!$blog->isLoggedIn()) {
    // redirect是在init.php中定义的快捷函数，用于页面跳转
    redirect('../login.php');
}

// 获取当前登录用户的信息，返回一个包含用户数据的数组
$currentUser = $blog->getCurrentUser();

// 从数据库获取网站的统计数据，包括文章数、用户数、评论数等
$stats = $blog->getStats();

// 从统计数据数组中提取各项数值，如果某项不存在就设为0
// ?? 是空合并运算符，当左边的值为null时使用右边的默认值
$totalArticles = $stats['total_articles'] ?? 0;  // 文章总数
$totalUsers = $stats['total_users'] ?? 0;        // 用户总数
$totalComments = $stats['total_comments'] ?? 0;  // 评论总数
$totalViews = $stats['total_views'] ?? 0;        // 总阅读量
$totalLikes = $stats['total_likes'] ?? 0;        // 总点赞数

// 获取最新的5篇文章，用于在管理后台显示
// 参数1是页码，参数5是每页显示数量
$recentArticles = $blog->getArticles(1, 5);

// 获取所有分类和标签的数量
// count()函数用来计算数组中元素的个数
$totalCategories = count($blog->getCategories());
$totalTags = count($blog->getTags());
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理后台 - 我的博客系统</title>
    <link rel="stylesheet" href="../assets/css/blog.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: "Microsoft YaHei", Arial, sans-serif;
            line-height: 1.6;
            color: #333;
            background-color: #f5f5f5;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        header {
            background: #fff;
            padding: 20px 0;
            margin-bottom: 30px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .logo {
            font-size: 24px;
            font-weight: bold;
            color: #2c3e50;
        }
        
        .nav {
            display: flex;
            gap: 20px;
        }
        
        .nav a {
            text-decoration: none;
            color: #333;
            padding: 8px 15px;
            border-radius: 5px;
            transition: background-color 0.3s;
        }
        
        .nav a:hover, .nav a.active {
            background-color: #3498db;
            color: white;
        }
        
        .admin-container {
            display: grid;
            grid-template-columns: 1fr;
            gap: 30px;
        }
        
        .welcome-section {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 40px;
            border-radius: 10px;
            text-align: center;
        }
        
        .welcome-title {
            font-size: 32px;
            margin-bottom: 10px;
        }
        
        .welcome-subtitle {
            font-size: 18px;
            opacity: 0.9;
        }
        
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .stat-card {
            background: white;
            padding: 30px 25px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            text-align: center;
            transition: transform 0.3s;
        }
        
        .stat-card:hover {
            transform: translateY(-5px);
        }
        
        .stat-number {
            font-size: 36px;
            font-weight: bold;
            margin-bottom: 10px;
        }
        
        .stat-label {
            color: #666;
            font-size: 16px;
        }
        
        .stat-card.articles .stat-number { color: #3498db; }
        .stat-card.users .stat-number { color: #27ae60; }
        .stat-card.comments .stat-number { color: #e74c3c; }
        .stat-card.views .stat-number { color: #f39c12; }
        .stat-card.likes .stat-number { color: #e91e63; }
        .stat-card.categories .stat-number { color: #9b59b6; }
        .stat-card.tags .stat-number { color: #1abc9c; }
        
        .content-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
        }
        
        .panel {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .panel-title {
            font-size: 24px;
            color: #2c3e50;
            margin-bottom: 20px;
            border-bottom: 2px solid #3498db;
            padding-bottom: 10px;
        }
        
        .quick-actions {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 15px;
        }
        
        .action-btn {
            background: #3498db;
            color: white;
            padding: 15px 20px;
            text-decoration: none;
            border-radius: 8px;
            text-align: center;
            transition: background-color 0.3s;
            font-weight: 500;
        }
        
        .action-btn:hover {
            background: #2980b9;
        }
        
        .action-btn.secondary {
            background: #95a5a6;
        }
        
        .action-btn.secondary:hover {
            background: #7f8c8d;
        }
        
        .action-btn.success {
            background: #27ae60;
        }
        
        .action-btn.success:hover {
            background: #229954;
        }
        
        .recent-articles {
            list-style: none;
        }
        
        .article-item {
            padding: 15px 0;
            border-bottom: 1px solid #e1e1e1;
        }
        
        .article-item:last-child {
            border-bottom: none;
        }
        
        .article-title {
            font-size: 16px;
            margin-bottom: 8px;
        }
        
        .article-title a {
            text-decoration: none;
            color: #2c3e50;
        }
        
        .article-title a:hover {
            color: #3498db;
        }
        
        .article-meta {
            color: #666;
            font-size: 14px;
            display: flex;
            justify-content: space-between;
        }
        
        .no-articles {
            text-align: center;
            color: #666;
            padding: 40px;
        }
        
        @media (max-width: 768px) {
            .content-grid {
                grid-template-columns: 1fr;
            }
            
            .stats-grid {
                grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            }
            
            .quick-actions {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <header>
        <div class="container">
            <div class="header-content">
                <div class="logo">我的博客系统</div>
                <nav class="nav">
                    <a href="../index.php">首页</a>
                    <a href="index.php" class="active">管理后台</a>
                    <a href="post.php">发布文章</a>
                    <a href="../logout.php">退出登录</a>
                </nav>
            </div>
        </div>
    </header>

    <div class="container">
        <div class="admin-container">
            <section class="welcome-section">
                <!-- 显示欢迎信息，包含当前登录用户的用户名 -->
                <!-- escape()函数用于防止XSS攻击，对用户输入进行HTML转义 -->
                <h1 class="welcome-title">欢迎回来，<?php echo escape($currentUser['用户名']); ?>！</h1>
                <p class="welcome-subtitle">欢迎使用博客管理系统，在这里您可以管理您的博客内容</p>
            </section>
            
            <div class="stats-grid">
                <!-- 显示文章总数统计卡片 -->
                <div class="stat-card articles">
                    <div class="stat-number"><?php echo $totalArticles; ?></div>
                    <div class="stat-label">文章总数</div>
                </div>
                
                <!-- 显示注册用户数统计卡片 -->
                <div class="stat-card users">
                    <div class="stat-number"><?php echo $totalUsers; ?></div>
                    <div class="stat-label">注册用户</div>
                </div>
                
                <!-- 显示评论总数统计卡片 -->
                <div class="stat-card comments">
                    <div class="stat-number"><?php echo $totalComments; ?></div>
                    <div class="stat-label">评论总数</div>
                </div>
                
                <!-- 显示总阅读量统计卡片 -->
                <div class="stat-card views">
                    <!-- number_format()函数用于格式化数字，添加千位分隔符 -->
                    <div class="stat-number"><?php echo number_format($totalViews); ?></div>
                    <div class="stat-label">总阅读量</div>
                </div>
                
                <!-- 显示总点赞数统计卡片 -->
                <div class="stat-card likes">
                    <div class="stat-number"><?php echo $totalLikes; ?></div>
                    <div class="stat-label">总点赞数</div>
                </div>
                
                <!-- 显示分类数量统计卡片 -->
                <div class="stat-card categories">
                    <div class="stat-number"><?php echo $totalCategories; ?></div>
                    <div class="stat-label">分类数量</div>
                </div>
                
                <!-- 显示标签数量统计卡片 -->
                <div class="stat-card tags">
                    <div class="stat-number"><?php echo $totalTags; ?></div>
                    <div class="stat-label">标签数量</div>
                </div>
            </div>
            
            <div class="content-grid">
                <div class="panel">
                    <h2 class="panel-title">快速操作</h2>
                    <div class="quick-actions">
                        <a href="post.php" class="action-btn success">发布新文章</a>
                        <a href="../index.php" class="action-btn">查看网站首页</a>
                        <a href="articles.php" class="action-btn secondary">管理文章</a>
                        <a href="../logout.php" class="action-btn secondary">退出登录</a>
                    </div>
                </div>
                
                <div class="panel">
                    <h2 class="panel-title">最新文章</h2>
                    <!-- 检查是否有文章数据 -->
                    <?php if (empty($recentArticles)): ?>
                        <!-- 如果没有文章，显示提示信息 -->
                        <div class="no-articles">
                            <p>暂无文章</p>
                            <p><a href="post.php" style="color: #3498db;">发布第一篇文章</a></p>
                        </div>
                    <?php else: ?>
                        <!-- 如果有文章，循环显示每篇文章 -->
                        <ul class="recent-articles">
                            <?php foreach ($recentArticles as $article): ?>
                                <li class="article-item">
                                    <div class="article-title">
                                        <!-- 文章标题链接，点击可查看文章详情 -->
                                        <a href="../article.php?id=<?php echo $article['id']; ?>">
                                            <!-- 对文章标题进行HTML转义防止XSS攻击 -->
                                            <?php echo escape($article['标题']); ?>
                                        </a>
                                    </div>
                                    <div class="article-meta">
                                        <!-- 显示文章发布时间，使用formatDate函数格式化 -->
                                        <span><?php echo formatDate($article['创建时间']); ?></span>
                                        <!-- 显示文章的阅读量、点赞量和评论量 -->
                                        <span>阅读：<?php echo $article['阅读量']; ?> | 点赞：<?php echo $article['点赞量']; ?> | 评论：<?php echo $article['评论量']; ?></span>
                                    </div>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</body>
</html> 